var math = require('./math.min');

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
// 根据时间戳判断发布时间---判断最新文章发布时间
const getDateDiff = date => {
  let fixDate = date.replace(/-/g, "/"); // 兼容ios日期格式
  let current = new Date(fixDate);
  let cMonth = current.getMonth() + 1;
  let cDay = current.getDate();
  let cSeconds = current.getTime();
  let result;

  let minute = 1000 * 60;
  let hour = minute * 60;
  let day = hour * 24

  let now = new Date().getTime();
  let diffValue = now - cSeconds;

  if (diffValue < 0) {
    return;
  }
  let dayC = diffValue / day;
  let hourC = diffValue / hour;
  let minC = diffValue / minute;
  if (dayC >= 1) {
      result = `${cMonth <= 9 ? '0'+cMonth : cMonth}-${cDay <= 9 ? '0'+cDay : cDay}`;
  } else if (hourC >= 1) {
      result = `${parseInt(hourC)}小时前`;
  } else if (minC >= 1) {
      result = `${parseInt(minC)}分钟前`;
  } else {
      result = '刚刚';
  }
  return result;
}
// 根据时间戳判断发布时间---判断栏目卡片发布时间
const getColumnDateDiff = date => {
  let fixDate = date.replace(/-/g, "/"); // 兼容ios日期格式
  let current = new Date(fixDate);
  let cMonth = current.getMonth() + 1;
  let cDay = current.getDate();
  let cHour = current.getHours();
  let cMinute = current.getMinutes();
  let cSeconds = current.getTime();
  let result;

  let minute = 1000 * 60;
  let hour = minute * 60;
  let day = hour * 24;

  let now = new Date().getTime();
  let diffValue = now - cSeconds;

  if (diffValue < 0) {
      return;
  }
  let dayC = diffValue / day;
  if (dayC >= 1) {
      result = `${cMonth <= 9 ? '0'+cMonth : cMonth}-${cDay <= 9 ? '0'+cDay : cDay}`;
  } else {
      result = `${cHour <= 9 ? '0'+ cHour : cHour}:${cMinute <= 9 ? '0'+cMinute : cMinute}`;
  }
  return result;
}

const formatDate = (date, option) => {
  let fixDate = date.replace(/-/g, "/"); // 兼容ios日期格式
  let current = new Date(fixDate);
  let year = current.getFullYear();
  let month = current.getMonth() + 1;
  let day = current.getDate();
  let hour = current.getHours();
  let minute = current.getMinutes();
  let second = current.getTime();
  
  //获取 年月日
  if (option == 'YY-MM-DD') return [year, month, day].map(formatNumber).join('-')
  
  //获取 月日
  if (option == 'MM-DD') return [month, day].map(formatNumber).join('-')
  
  //数据页面表头日期
  if (option == 'M月D日') return `${month+'月'+day+'日'}`
  
  //获取 月日时分
  if (option == 'MM-DD HH:mm') return [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')

  //获取 时分秒
  if (option == 'hh-mm-ss') return [hour, minute, second].map(formatNumber).join(':')

  //获取 时分
  if (option == 'hh-mm') return [hour, minute].map(formatNumber).join(':')

  //获取 分秒
  if (option == 'mm-dd') return [minute, second].map(formatNumber).join(':')

  //默认 时分秒 年月日
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// 报价单位
const quoteUnitFilter = value => {
  switch (value) {
    case 'yuan_ton':  return '元/吨';
    case 'yuan_kg':   return '元/千克';
    case 'metal_ton': return '元/金属吨';
    case 'yuan_ni':   return '元/镍点';
    case 'usd_ton':   return '美元/吨';
    case 'dry_ton':   return '美元/干吨';
    case 'wet_ton':   return '美元/湿吨';
    case '%':         return '%';
    case 'yuan_pieces': return '元/只';
    case 'yuan_set':  return '元/组';
    case '-':         return '-';
    case 'wan_yuan_ton': return '万元/吨';
  }
}

// 发票
const invoiceTaxFilter = v => {
  switch (v) {
    case '':
    case null:
      return '空';
    case 'true':
    case true:
      return '含税';
    case 'false':
    case false:
      return '不含税';
  }
}
// 金属类型名称
const metalNameFilter = value => {
  switch (value) {
    case 'cu':
        return '铜';
    case 'al':
        return '铝';
    case 'zn':
        return '锌';
    case 'pb':
        return '铅';
    case 'sn':
        return '锡';
    case 'ni':
        return '镍';
    case 'ala':
        return '铝合金';
    case 'wrm':
        return '线材';
    case 'hcm':
        return '热轧卷板';
    case 'ssm':
        return '不锈钢';
    case 'rbm':
        return '螺纹钢';
  }
}
// 金属名称转换
const metalName2Type = value => {
  switch (value) {
    case '铜':
      return 'cu';
    case '铝':
      return 'al';
    case '锌':
      return 'zn';
    case '铅':
      return 'pb';
    case '锡':
      return 'sn';
    case '镍':
      return 'ni';
    case '铝合金':
      return 'ala';
    case '线材':
      return 'wrm';
    case '热轧卷板':
      return 'hcm';
    case '不锈钢':
      return 'ssm';
    case '螺纹钢':
      return 'rbm';
  }
}
const bigNumberFixed = (number, digit) => {
  let bigNumber = math.number(
    math.chain(math.bignumber(number))
            .multiply(math.bignumber(Math.pow(10, digit)))
            .done()
  );
  if (number<0) {
    return -((Math.round(-bigNumber) / Math.pow(10, digit))).toFixed(digit);
  }else{
    return (Math.round(bigNumber) / Math.pow(10, digit)).toFixed(digit);
  }
}
// 千分位显示
const Thousandth = (value) => {
  if (!value) return;
  let n1 = value.toString().replace(/\d+/, function(n){ // 先提取整数部分
      return n.replace(/(\d)(?=(\d{3})+$)/g,function($1){
          return $1+",";
      });
  })
  // let re = /\d{1,3}(?=(\d{3})+$)/g;
  // let n1 = value.replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,")+s2;});
  return n1;
}
const safePhoneNumber = (value) => {
  if (!value) return;
  return value.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2');
}
// cookie无操作超时时长：168小时(7天) 604800秒
const expiresIn = 604800000;
module.exports = {
  formatTime, getDateDiff, getColumnDateDiff, formatDate,
  bigNumberFixed, quoteUnitFilter, invoiceTaxFilter, 
  metalNameFilter, metalName2Type, Thousandth, safePhoneNumber,
  expiresIn
}
